用Java进行数据库连接,Connection con = new Connection(urlName);时,出现编译错误
发布网友
发布时间:2022-04-15 05:06
我来回答
共2个回答
热心网友
时间:2022-04-15 06:35
是这个样子的,
public static Connection getDconn() throws Exception{
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
}catch(Exception ex){
throw new Exception("数据库连接失败");
}
return conn;
}
当然不能那样了.
热心网友
时间:2022-04-15 07:53
jdbc操作如下:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection(urlName);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from 表");
while(rs.next()){
System.out.println(rs.getString("字段"));
}