java怎么调用mysql数据库连接
发布网友
发布时间:2022-05-07 07:12
我来回答
共3个回答
懂视网
时间:2022-05-07 11:33
mysqljava数据库
比如用户注册,要怎么调用数据库里面的数据以此来判断用户名有没有被注册
热心网友
时间:2022-05-07 08:41
package wb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class Connect {
String DBDRIVER="com.mysql.jdbc.Driver";
String DBURL="jdbc:mysql://localhost:3306/test";
String DBUSER="root";
String DBPWD="1995nian";
Connection conn=null;
public Connect(){
try {
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPWD);
//System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
public Connection getCon(){
return conn;
}
public void doSql(double x,double y){
Connection connection=new Connect().getCon();
try {
String sql="insert into tmp values(?,?)";
PreparedStatement ps=connection.prepareStatement(sql);
ps.setDouble(1, x);
ps.setDouble(2, y);
ps.execute();
ps.close();
connection.close();
} catch (Exception e) {
// TODO: handle exception
}
}
public void closeCon(){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String[] args){
Connect connect=new Connect();
Connection connection=connect.getCon();
//String sql="insert into tmp values(30,50)";
connect.doSql(30, 50);
}
}
这是样板,项目还要引入mysql的数据库驱动
热心网友
时间:2022-05-07 09:59
https://zhidao.baidu.com/question/264734748166047805.html?fr=iks&word=java+mysql%CA%FD%BE%DD%BF%E2&ie=gbk
https://zhidao.baidu.com/question/458666899.html?fr=iks&word=java+mysql%CA%FD%BE%DD%BF%E2&ie=gbk
两个 是可以参考 的
~~~~~~~