发布网友 发布时间:2022-04-23 12:34
共3个回答
热心网友 时间:2022-04-08 12:19
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=GBK";
String name="root";
String pass="mysql密码";
conn=DriverManager.getConnection(url,name,pass);
热心网友 时间:2022-04-08 13:37
JDBC连接数据库并建表
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCConnection {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//注册,加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//数据库连接
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","scott");
//执行语句
Statement state=con.createStatement();//创建Statement对象
String sql="create table TEACHER(tno varchar2(10),tname char(10))";//创建TEACHER表
state.executeUpdate(sql);//执行sql语句
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
热心网友 时间:2022-04-08 15:11
楼上已写